package ru.bragnikita.utils.swing.utils;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author Nikita.Brazhnikov
 */
public class ValueUtils {

	public static boolean getBoolean(Object o) {
		if (o == null)
			return false;
		if (o instanceof Boolean)
			return (Boolean) o;
		final String str = StringUtils.lowerCase(ObjectUtils.toString(o));
		if ("true".equals(str))
			return true;
		if ("false".equals(str))
			return false;
		int intVal = getInt(o);
		return intVal != 0;
	}

	public static int getInt(Object o) {
		if (o == null)
			return 0;
		if (o instanceof Number)
			return ((Number) o).intValue();
		String strVal = ObjectUtils.toString(o);
		try {
			return Integer.parseInt(strVal);
		} catch (NumberFormatException ignored){}
		return 0;
	}

}
